#include "app_modbus_rtu.h"

/*************************extern******************************************/
extern dev_config dev_config_info;
/*************************rt_thread***************************************/
struct rt_thread modbus_rtu_thread;//modbus tcp
/*************************rt_stack****************************************/
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t modbus_rtu_thread_stack[2048];

struct rt_semaphore sem_modbusrtu_available;

#define RS485_RE GET_PIN(D, 4)

MODBUS_RTU_DATA  MODBUSRTU = {0};

static void thread_modbus_rtu_entry(void *parameter)	
{
    modbus_t *ctx = RT_NULL;
    ctx = modbus_new_rtu("/dev/uart7", 115200, 'N', 8, 1);
    modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
    modbus_rtu_set_rts(ctx, RS485_RE, MODBUS_RTU_RTS_UP);
    modbus_set_slave(ctx, 1);
    modbus_connect(ctx);
    modbus_set_response_timeout(ctx, 0, 1000000);
    while (1)
    {
				rt_sem_take(&sem_modbusrtu_available,RT_WAITING_FOREVER);
        modbus_read_registers(ctx, 0, AIR_STA_LEN, MODBUSRTU.air_sta);
			  rt_sem_release(&sem_modbusrtu_available);
        rt_thread_mdelay(1000);	
    }
}
/*******************************************************************************
* Function Name  : app_init_modbus_rtu
* Description    : 
* Input          : void
* Output         : None
* Return         : None
* Attention		   : None
*******************************************************************************/
int app_init_modbus_rtu(void)
{
	rt_pin_mode(RS485_RE, PIN_MODE_OUTPUT);
	rt_sem_init(&sem_modbusrtu_available, "modbus_r", 1, RT_IPC_FLAG_PRIO);		
   
	rt_thread_init(&modbus_rtu_thread,                		
								 "modbus_rtu",                        		
								 thread_modbus_rtu_entry,           	 	
								 RT_NULL,                     		  		
								 &modbus_rtu_thread_stack[0],      
								 sizeof(modbus_rtu_thread_stack),   
								 modbus_rtu_pro,    								    
								 20);                         		  		
	rt_thread_startup(&modbus_rtu_thread);            
								 
	return RT_EOK;
}

INIT_APP_EXPORT(app_init_modbus_rtu);



